package game;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import java.awt.Font;

public class GameServer extends JFrame implements Runnable{

	ServerSocket server = null;
	Socket client = null;
	boolean threadFlag = true;
	int cnt = 0;
	BufferedReader receive = null;
	BufferedWriter send = null;
	String mycho = "";
	String youcho = "";
	int win = 0, lose = 0, draw = 0;
	ServerCount sc;
	
	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_1;
	JButton btnServerStart;
	JButton btnServerStop;
	JButton btnGameStart;
	private JPanel panel_2;
	private JPanel panel_4;
	private JPanel panel_5;
	private JPanel panel_6;
	private JPanel panel_7;
	private JLabel score;
	private JPanel panel_8;
	private JPanel panel_9;
	private JPanel panel_10;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	JLabel myFace;
	JLabel youFace;
	JLabel myChoice;
	JLabel youChoice;
	private JPanel panel_3;
	private JButton btnGawi;
	private JButton btnBawi;
	private JButton btnBo;
	private JLabel lblNewLabel_4;
	private JLabel lblNewLabel_5;
	JLabel count;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GameServer frame = new GameServer();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GameServer() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 699, 521);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getPanel_2(), BorderLayout.SOUTH);
		contentPane.add(getPanel_4(), BorderLayout.WEST);
		contentPane.add(getPanel_7(), BorderLayout.CENTER);
		int i = (int)(Math.random()*10)+1;
		int j = (int)(Math.random()*10)+1;
		myChoice.setText("");
		youChoice.setText("");
		myChoice.setIcon(new ImageIcon("image\\imo"+i+".jpg"));
		youChoice.setIcon(new ImageIcon("image\\imo"+j+".jpg"));
		
		try {
			InetAddress ia = InetAddress.getLocalHost();
			setTitle("서버 IP: "+ia.getHostAddress()+" / port : 4444");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
	public void sendMsg(String ms){
		try{
			send.write(ms);
			send.newLine();
			send.flush();
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void serverStart(){
		try{
		
		Thread t = new Thread(this);
		t.start();

		btnServerStart.setEnabled(false);
		btnServerStop.setEnabled(true);
			
		
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void serverStop(){
		btnServerStart.setEnabled(true);
		btnServerStop.setEnabled(false);
		btnGameStart.setEnabled(false);
		btnGawi.setEnabled(false);
		btnBawi.setEnabled(false);
		btnBo.setEnabled(false);
		try {
			sendMsg("serverStop");
			server.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		threadFlag = false;
		sc.stop();
		count.setText("");
		youcho = "";
		mycho = "";
		win=0;lose=0;draw=0;
		int i = (int)(Math.random()*10)+1;
		int j = (int)(Math.random()*10)+1;
		myChoice.setText("");
		youChoice.setText("");
		myChoice.setIcon(new ImageIcon("image\\imo"+i+".jpg"));
		youChoice.setIcon(new ImageIcon("image\\imo"+j+".jpg"));
		youFace.setIcon(new ImageIcon(""));
		
	}
	
	public void gameStart() {
		sendMsg("Start");
		sc = new ServerCount(this);
		sc.start();
		btnGawi.setEnabled(true);
		btnBawi.setEnabled(true);
		btnBo.setEnabled(true);
		int i = (int)(Math.random()*10)+1;
		int j = (int)(Math.random()*10)+1;
		myChoice.setText("");
		youChoice.setText("");
		myChoice.setIcon(new ImageIcon("image\\imo"+i+".jpg"));
		youChoice.setIcon(new ImageIcon("image\\imo"+j+".jpg"));

	}
	public void setResult(){
		
		if (!("".equals(mycho)) && !("".equals(youcho))) {
			myChoice.setIcon(new ImageIcon("image\\" + mycho + ".jpg"));
			youChoice.setIcon(new ImageIcon("image\\" + youcho + ".jpg"));
			
			if ("ga".equals(mycho) && "ga".equals(youcho)) draw++;
			else if ("ba".equals(mycho) && "ba".equals(youcho)) draw++;
			else if ("bo".equals(mycho) && "bo".equals(youcho)) draw++;
			
			else if ("ga".equals(mycho) && "bo".equals(youcho)) win++;
			else if ("ba".equals(mycho) && "ga".equals(youcho)) win++;
			else if ("bo".equals(mycho) && "ba".equals(youcho)) win++;
			
			else if ("ga".equals(mycho) && "ba".equals(youcho)) lose++;
			else if ("ba".equals(mycho) && "bo".equals(youcho)) lose++;
			else if ("bo".equals(mycho) && "ga".equals(youcho)) lose++;

			
			sc.stop();//카운트 종료
			count.setText("");
			score.setText("전적 승:" + win + " 패:" + lose + " 무:" + draw);
			sendMsg(youcho + mycho);
			btnGameStart.setEnabled(true);
			youcho = "";
			mycho = "";
		}
	}
	public void timeOver(){
		if("aa".equals(mycho)){
			myChoice.setIcon(new ImageIcon(""));
			youChoice.setIcon(new ImageIcon(""));
			myChoice.setText("시간");
			youChoice.setText("초과");
		}else if ("bb".equals(mycho)) {
			myChoice.setIcon(new ImageIcon(""));
			youChoice.setIcon(new ImageIcon(""));
			myChoice.setText("패");
			youChoice.setText("배");
		} else if ("cc".equals(mycho)) {
			myChoice.setIcon(new ImageIcon(""));
			youChoice.setIcon(new ImageIcon(""));
			myChoice.setText("승");
			youChoice.setText("리");
		}
		
		score.setText("전적 승:" + win + " 패:" + lose + " 무:" + draw);
		sendMsg(youcho + mycho);
		btnGameStart.setEnabled(true);
		youcho = "";
		mycho = "";
		btnGawi.setEnabled(false);
		btnBawi.setEnabled(false);
		btnBo.setEnabled(false);
		
	}
	
	@Override
	public void run() {
		try {
			server = new ServerSocket(4444);
			client = server.accept();
			btnGameStart.setEnabled(true);
			OutputStream os = client.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os);
			send = new BufferedWriter(osw);

			InputStream is = client.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			receive = new BufferedReader(isr);

			sendMsg("cho");
			threadFlag = true;

			while (threadFlag) {
				String str = receive.readLine();

				if ("bin".equals(str)) {
					youFace.setIcon(new ImageIcon("image\\bin.jpg"));
				}else if("Disconnect".equals(str)){
					threadFlag = false;
					btnGameStart.setEnabled(false);
					sc.stop();
					count.setText("");
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
					youcho = "";
					mycho = "";
					win=0;lose=0;draw=0;
					myChoice.setText("");
					youChoice.setText("");
					int i = (int)(Math.random()*10)+1;
					int j = (int)(Math.random()*10)+1;
					myChoice.setIcon(new ImageIcon("image\\imo"+i+".jpg"));
					youChoice.setIcon(new ImageIcon("image\\imo"+j+".jpg"));
					youFace.setIcon(new ImageIcon(""));
				}

				if ("ga".equals(str)) {
					youcho = "ga";
				} else if ("ba".equals(str)) {
					youcho = "ba";
				} else if ("bo".equals(str)) {
					youcho = "bo";
				}
				
				if(!("".equals(mycho))&&!("".equals(youcho))){
					setResult();
				}
				
			}

			btnServerStart.setEnabled(true);
			btnServerStop.setEnabled(false);

			receive.close();
			isr.close();
			is.close();

			send.close();
			osw.close();
			os.close();

			client.close();
			btnServerStart.setEnabled(true);
			btnServerStop.setEnabled(false);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getPanel_1(), BorderLayout.EAST);
			panel.add(getBtnGameStart(), BorderLayout.CENTER);
			panel.add(getCount(), BorderLayout.WEST);
		}
		return panel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.add(getBtnServerStart());
			panel_1.add(getBtnServerStop());
		}
		return panel_1;
	}
	private JButton getBtnServerStart() {
		if (btnServerStart == null) {
			btnServerStart = new JButton("\uC11C\uBC84 \uC2DC\uC791");
			btnServerStart.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					serverStart();
				}
			});
		}
		return btnServerStart;
	}
	private JButton getBtnServerStop() {
		if (btnServerStop == null) {
			btnServerStop = new JButton("\uC11C\uBC84 \uC885\uB8CC");
			btnServerStop.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverStop();
				}
			});
			btnServerStop.setEnabled(false);
		}
		return btnServerStop;
	}
	private JButton getBtnGameStart() {
		if (btnGameStart == null) {
			btnGameStart = new JButton("\uAC8C\uC784\uC2DC\uC791");
			btnGameStart.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					gameStart();
					btnGameStart.setEnabled(false);
				}
			});
			btnGameStart.setEnabled(false);
		}
		return btnGameStart;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setLayout(new GridLayout(0, 1, 0, 0));
			panel_2.add(getPanel_3_1());
		}
		return panel_2;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setLayout(new GridLayout(0, 1, 0, 0));
			panel_4.add(getPanel_5());
			panel_4.add(getPanel_6());
		}
		return panel_4;
	}
	private JPanel getPanel_5() {
		if (panel_5 == null) {
			panel_5 = new JPanel();
			panel_5.setLayout(new BorderLayout(0, 0));
			panel_5.add(getMyFace(), BorderLayout.CENTER);
			panel_5.add(getLblNewLabel_4(), BorderLayout.NORTH);
		}
		return panel_5;
	}
	private JPanel getPanel_6() {
		if (panel_6 == null) {
			panel_6 = new JPanel();
			panel_6.setLayout(new BorderLayout(0, 0));
			panel_6.add(getYouFace(), BorderLayout.CENTER);
			panel_6.add(getLblNewLabel_5(), BorderLayout.NORTH);
		}
		return panel_6;
	}
	private JPanel getPanel_7() {
		if (panel_7 == null) {
			panel_7 = new JPanel();
			panel_7.setLayout(new BorderLayout(0, 0));
			panel_7.add(getScore(), BorderLayout.NORTH);
			panel_7.add(getPanel_8(), BorderLayout.CENTER);
		}
		return panel_7;
	}
	private JLabel getScore() {
		if (score == null) {
			score = new JLabel("\uC804\uC801");
			score.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return score;
	}
	private JPanel getPanel_8() {
		if (panel_8 == null) {
			panel_8 = new JPanel();
			panel_8.setLayout(new GridLayout(0, 2, 0, 0));
			panel_8.add(getPanel_9());
			panel_8.add(getPanel_10());
		}
		return panel_8;
	}
	private JPanel getPanel_9() {
		if (panel_9 == null) {
			panel_9 = new JPanel();
			panel_9.setLayout(new BorderLayout(0, 0));
			panel_9.add(getLblNewLabel_2(), BorderLayout.NORTH);
			panel_9.add(getMyChoice(), BorderLayout.CENTER);
		}
		return panel_9;
	}
	private JPanel getPanel_10() {
		if (panel_10 == null) {
			panel_10 = new JPanel();
			panel_10.setLayout(new BorderLayout(0, 0));
			panel_10.add(getLblNewLabel_3(), BorderLayout.NORTH);
			panel_10.add(getYouChoice(), BorderLayout.CENTER);
		}
		return panel_10;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("\uB098\uC758 \uC120\uD0DD");
			lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("\uC0C1\uB300\uBC29 \uC120\uD0DD");
			lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel_3;
	}
	private JLabel getMyFace() {
		if (myFace == null) {
			myFace = new JLabel("");
			myFace.setHorizontalAlignment(SwingConstants.CENTER);
			myFace.setIcon(new ImageIcon("image\\cho.jpg"));
		}
		return myFace;
	}
	private JLabel getYouFace() {
		if (youFace == null) {
			youFace = new JLabel("");
			youFace.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return youFace;
	}
	private JLabel getMyChoice() {
		if (myChoice == null) {
			myChoice = new JLabel("");
			myChoice.setFont(new Font("궁서", Font.PLAIN, 55));
			myChoice.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return myChoice;
	}
	private JLabel getYouChoice() {
		if (youChoice == null) {
			youChoice = new JLabel("");
			youChoice.setFont(new Font("궁서", Font.PLAIN, 55));
			youChoice.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return youChoice;
	}
	private JPanel getPanel_3_1() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setLayout(new GridLayout(0, 3, 0, 0));
			panel_3.add(getBtnGawi());
			panel_3.add(getBtnBawi());
			panel_3.add(getBtnBo());
		}
		return panel_3;
	}
	private JButton getBtnGawi() {
		if (btnGawi == null) {
			btnGawi = new JButton("가위");
			btnGawi.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					mycho="ga";
					myChoice.setIcon(new ImageIcon("image\\ga.jpg"));
					setResult();
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
					
				}
			});
			btnGawi.setEnabled(false);
		}
		return btnGawi;
	}
	private JButton getBtnBawi() {
		if (btnBawi == null) {
			btnBawi = new JButton("바위");
			btnBawi.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					mycho="ba";
					myChoice.setIcon(new ImageIcon("image\\ba.jpg"));
					setResult();
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
					}
			});
			btnBawi.setEnabled(false);
		}
		return btnBawi;
	}
	private JButton getBtnBo() {
		if (btnBo == null) {
			btnBo = new JButton("\uBCF4");
			btnBo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					mycho="bo";
					myChoice.setIcon(new ImageIcon("image\\bo.jpg"));
					setResult();
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
					}
			});
			btnBo.setEnabled(false);
		}
		return btnBo;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("\uB0B4 \uC774\uBBF8\uC9C0");
		}
		return lblNewLabel_4;
	}
	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("\uC0C1\uB300\uBC29 \uC774\uBBF8\uC9C0");
		}
		return lblNewLabel_5;
	}
	private JLabel getCount() {
		if (count == null) {
			count = new JLabel(" ");
			count.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return count;
	}
}
